//
//  ContentView.m
//  开眼
//
//  Created by juvham on 16/1/21.
//  Copyright © 2016年 juvham. All rights reserved.
//

#import "ContentView.h"
#import "EveryDayModel.h"
#import "CustomView.h"

@interface ContentView ()
@property (nonatomic, strong) EveryDayModel *model;
@end

@implementation ContentView

- (instancetype)initWithFrame:(CGRect)frame Width:(CGFloat)width model:(EveryDayModel *)model collor:(UIColor *)collor{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.model = [[EveryDayModel alloc] init];
        self.model = model;
        
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;

        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageView.userInteractionEnabled = YES;
        [self addSubview:_imageView];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // 标题
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, kWidth, 30)];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textColor = collor;
        [self addSubview:_titleLabel];
        
        // 白线
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(5, 35, 200, 1)];
        _lineView.backgroundColor = collor;
        [self addSubview:_lineView];
        
        // 类型
        _littleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, _lineView.frame.origin.y + 5, kWidth, 20)];
        
        _littleLabel.textColor = collor;
        _littleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_littleLabel];
        
        
        // 判断机型, 给frame
        NSString *iphone = [CheckPhone getCurrentDevice];
        if ([iphone isEqualToString:@"iPhone 4S (A1387/A1431)"]) {
            _descripLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, _littleLabel.frame.origin.y + _littleLabel.frame.size.height, kWidth - 10, self.frame.size.height / 3)];
        } else {
            // 正文
            _descripLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, _littleLabel.frame.origin.y + _littleLabel.frame.size.height, kWidth - 10, self.frame.size.height / 2)];
        }
        
        
      //  _descripLabel.backgroundColor = [UIColor redColor];
        
        _descripLabel.numberOfLines = 0;
        _descripLabel.textColor = collor;
        [self addSubview:_descripLabel];
        
        
        CGFloat y = _descripLabel.frame.origin.y + _descripLabel.frame.size.height;
        
        // 喜欢
        _collectionCustom = [[CustomView alloc]initWithFrame:CGRectMake(5, y, (kWidth - 10) / 4, 30) Width:width LabelString:[model.consumption objectForKey:@"collectionCount"] collor:collor];
        
        [self addSubview:_collectionCustom];
        
        
        
        // 分享
        _shareCustom = [[CustomView alloc]initWithFrame:CGRectMake((kWidth - 10) / 4 + 5, y, (kWidth - 10) / 4, 30) Width:width LabelString:[model.consumption objectForKey:@"shareCount"] collor:collor];
        [self addSubview:_shareCustom];
        [_shareCustom.button setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
        
        // 评论
        _replyCustom = [[CustomView alloc]initWithFrame:CGRectMake(_shareCustom.frame.origin.x + _shareCustom.frame.size.width, y, (kWidth - 10) / 4, 30) Width:width LabelString:[model.consumption objectForKey:@"replyCount"] collor:collor];
        [self addSubview:_replyCustom];
        [_replyCustom.button setBackgroundImage:[UIImage imageNamed:@"iconfont-pinglun"] forState:UIControlStateNormal];
        // 缓存
        _cacheCustom = [[CustomView alloc]initWithFrame:CGRectMake(_replyCustom.frame.origin.x + _replyCustom.frame.size.width, y, (kWidth - 10) / 4, 30) Width:width LabelString:@"缓存" collor:collor];
        [self addSubview:_cacheCustom];
        [_cacheCustom.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiazai"] forState:UIControlStateNormal];
        
        
        

        [_imageView sd_setImageWithURL:[NSURL URLWithString:[model.cover objectForKey:@"feed"]]];

        [self setData:model];
        
        
        // 判断机型, 给字体
        
         if ([iphone isEqualToString:@"iPhone 4S (A1387/A1431)"]) {
             // 正文的字号
             _descripLabel.font = [UIFont systemFontOfSize:10];
             _cacheCustom.label.font = [UIFont systemFontOfSize:10];
         } else {
             _descripLabel.font = [UIFont systemFontOfSize:17];
         }
    }
    [self.collectionCustom.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return self;
}



#pragma mark - 收藏Action
- (void)buttonAction:(UIButton *)button {
    
    button.selected ^= 1;
    
    
    if (button.selected) {
        [button setBackgroundImage:[UIImage imageNamed:@"iconfont-xin(hongse)"] forState:UIControlStateNormal];
        [[DataBaseHandler shareDataBaseHandler] insertModelWriteRead:self.model];
    } else {
        [button setBackgroundImage:[UIImage imageNamed:@"iconfont-xin(baise)"] forState:UIControlStateNormal];
        [[DataBaseHandler shareDataBaseHandler] deleteWithTitle:self.model.title];
    }
    
}

- (void)setData:(EveryDayModel *)model {

    // 判断是否收藏
    if ([[[DataBaseHandler shareDataBaseHandler] selectForTitle:model.title] count]) {
        
        [_collectionCustom.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xin(hongse)"] forState:UIControlStateNormal];
        _collectionCustom.button.selected = YES;
    } else {
        [_collectionCustom.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xin(baise)"] forState:UIControlStateNormal];
        _collectionCustom.button.selected = NO;
    }

    
    self.titleLabel.text = model.title;
    self.descripLabel.text = model.descrip;
    [self.shareCustom setTitle:[model.consumption objectForKey:@"shareCount"]];
    [self.replyCustom setTitle:[model.consumption objectForKey:@"replyCount"]];
    [self.collectionCustom setTitle:[model.consumption objectForKey:@"collectionCount"]];

    NSInteger time = [model.duration integerValue];
    NSString *timeString = [NSString stringWithFormat:@"%02ld'%02ld''",time/60,time% 60];//显示的是音乐的总时间
    NSString *string = [NSString stringWithFormat:@"#%@ / %@",model.category, timeString];
    self.littleLabel.text = string;


    __weak typeof(self) weakSelf = self;


    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[model.cover objectForKey:@"blurred"]] options:(SDWebImageRetryFailed) progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

        if (image) {
            
            CABasicAnimation *contentsAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
            contentsAnimation.duration = 1.0f;
            contentsAnimation.fromValue = self.imageView.image ;
            contentsAnimation.toValue = image;

            contentsAnimation.removedOnCompletion = YES;
            contentsAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [weakSelf.imageView.layer addAnimation:contentsAnimation forKey:nil];

            weakSelf.imageView.image = image;

        }

    }];
}


@end
